home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Win95 Secrets / SETUP.Z / PEDUMP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-19  |  4.4 KB  |  157 lines

  1. //==================================
  2. // PEDUMP - Matt Pietrek 1995
  3. // FILE: PEDUMP.C
  4. //==================================
  5.  
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include "objdump.h"
  9. #include "exedump.h"
  10. #include "dbgdump.h"
  11. #include "libdump.h"
  12. #include "extrnvar.h"
  13.  
  14. // Global variables set here, and used in EXEDUMP.C and OBJDUMP.C
  15. BOOL fShowRelocations = FALSE;
  16. BOOL fShowRawSectionData = FALSE;
  17. BOOL fShowSymbolTable = FALSE;
  18. BOOL fShowLineNumbers = FALSE;
  19. BOOL fShowIATentries = FALSE;
  20.  
  21. char HelpText[] = 
  22. "PEDUMP - Win32/COFF EXE/OBJ/LIB file dumper - 1995 Matt Pietrek\n\n"
  23. "Syntax: PEDUMP [switches] filename\n\n"
  24. "  /A    include everything in dump\n"
  25. "  /H    include hex dump of sections\n"
  26. "  /I    include Import Address Table thunk addresses\n"
  27. "  /L    include line number information\n"
  28. "  /R    show base relocations\n"
  29. "  /S    show symbol table\n";
  30.  
  31. //
  32. // Open up a file, memory map it, and call the appropriate dumping routine
  33. //
  34. void DumpFile(LPSTR filename)
  35. {
  36.     HANDLE hFile;
  37.     HANDLE hFileMapping;
  38.     LPVOID lpFileBase;
  39.     PIMAGE_DOS_HEADER dosHeader;
  40.     
  41.     hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
  42.                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  43.                     
  44.     if ( hFile == INVALID_HANDLE_VALUE )
  45.     {
  46.         printf("Couldn't open file with CreateFile()\n");
  47.         return;
  48.     }
  49.  
  50.     hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  51.     if ( hFileMapping == 0 )
  52.     {
  53.         CloseHandle(hFile);
  54.         printf("Couldn't open file mapping with CreateFileMapping()\n");
  55.         return;
  56.     }
  57.  
  58.     lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);
  59.     if ( lpFileBase == 0 )
  60.     {
  61.         CloseHandle(hFileMapping);
  62.         CloseHandle(hFile);
  63.         printf("Couldn't map view of file with MapViewOfFile()\n");
  64.         return;
  65.     }
  66.  
  67.     printf("Dump of file %s\n\n", filename);
  68.     
  69.     dosHeader = (PIMAGE_DOS_HEADER)lpFileBase;
  70.     if ( dosHeader->e_magic == IMAGE_DOS_SIGNATURE )
  71.     {
  72.         DumpExeFile( dosHeader );
  73.     }
  74.     else if ( (dosHeader->e_magic == IMAGE_DBG_SIGNATURE) && 
  75.               (dosHeader->e_cblp == 0) )
  76.     {
  77.         DumpDbgFile( (PIMAGE_DBG_HEADER)lpFileBase );
  78.     }
  79.     else if ( (dosHeader->e_magic == 0x014C)    // Does it look like a i386
  80.               && (dosHeader->e_sp == 0) )       // COFF OBJ file???
  81.     {
  82.         // The two tests above aren't what they look like.  They're
  83.         // really checking for IMAGE_FILE_HEADER.Machine == i386 (0x14C)
  84.         // and IMAGE_FILE_HEADER.SizeOfOptionalHeader == 0;
  85.             
  86.         DumpObjFile( (PIMAGE_FILE_HEADER)lpFileBase );
  87.     }
  88.     else if ( 0 == strncmp(lpFileBase, IMAGE_ARCHIVE_START,
  89.                                         IMAGE_ARCHIVE_START_SIZE ) )
  90.     {
  91.         DumpLibFile( lpFileBase );
  92.     }
  93.     else
  94.         printf("unrecognized file format\n");
  95.     UnmapViewOfFile(lpFileBase);
  96.     CloseHandle(hFileMapping);
  97.     CloseHandle(hFile);
  98. }
  99.  
  100. //
  101. // process all the command line arguments and return a pointer to
  102. // the filename argument.
  103. //
  104. PSTR ProcessCommandLine(int argc, char *argv[])
  105. {
  106.     int i;
  107.     
  108.     for ( i=1; i < argc; i++ )
  109.     {
  110.         strupr(argv[i]);
  111.         
  112.         // Is it a switch character?
  113.         if ( (argv[i][0] == '-') || (argv[i][0] == '/') )
  114.         {
  115.             if ( argv[i][1] == 'A' )
  116.             {
  117.                 fShowRelocations = TRUE;
  118.                 fShowRawSectionData = TRUE;
  119.                 fShowSymbolTable = TRUE;
  120.                 fShowLineNumbers = TRUE;
  121.                 fShowIATentries = TRUE;
  122.             }
  123.             else if ( argv[i][1] == 'H' )
  124.                 fShowRawSectionData = TRUE;
  125.             else if ( argv[i][1] == 'L' )
  126.                 fShowLineNumbers = TRUE;
  127.             else if ( argv[i][1] == 'R' )
  128.                 fShowRelocations = TRUE;
  129.             else if ( argv[i][1] == 'S' )
  130.                 fShowSymbolTable = TRUE;
  131.             else if ( argv[i][1] == 'I' )
  132.                 fShowIATentries = TRUE;
  133.         }
  134.         else    // Not a switch character.  Must be the filename
  135.         {
  136.             return argv[i];
  137.         }
  138.     }
  139. }
  140.  
  141. int main(int argc, char *argv[])
  142. {
  143.     PSTR filename;
  144.     
  145.     if ( argc == 1 )
  146.     {
  147.         printf( HelpText );
  148.         return 1;
  149.     }
  150.     
  151.     filename = ProcessCommandLine(argc, argv);
  152.     if ( filename )
  153.         DumpFile( filename );
  154.  
  155.     return 0;
  156. }
  157.